package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.bean.Shopping;
import com.huawei.petSystemdemo.ui.notifications.NotificationsFragment;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.MyRequest;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.ShoppingViewHolder> {

    private List<Shopping> list;
    Context context;
    private Map<String, Object> resBody = new HashMap<>();
    private Shop shop;
    private Pets pets;

    public ShoppingAdapter(List<Shopping> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public ShoppingViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_shopping, parent, false);
        return new ShoppingAdapter.ShoppingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ShoppingViewHolder holder, int position) {

        Shopping shopping = list.get(position);

        resBody.put("id", shopping.getPetId());

        MyRequest.get("/pet/detailed", resBody, json -> {
            JSONObject obj = JSON.parseObject(json);
            int status = obj.getInteger("statusCode");
            if(status != 200) {
                System.out.println("wrong");
            } else {
                pets = obj.getObject("data", Pets.class);

                resBody.clear();
                resBody.put("id", pets.getShopId());
                MyRequest.get("/shop/getOne", resBody, json1 -> {
                    JSONObject jsonObject = JSON.parseObject(json1);
                    int status1 = jsonObject.getInteger("statusCode");
                    if(status1 != 200) {
                        System.out.println("wrong");
                    } else {
                        shop = jsonObject.getObject("data", Shop.class);
                        holder.shopping_shop_name.setText(shop.getName());
                    }
                });

                holder.shopping_name.setText(pets.getName());
                holder.shopping_price.setText(pets.getPrice() +"");

                ImgRepository.getImage("/pet/getImg?img=" + pets.getImg(), new Consumer<Bitmap>() {

                    @Override
                    public void accept(Bitmap bitmap) {
                        holder.shopping_img.setImageBitmap(bitmap);
                    }
                });

                holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        // TODO Auto-generated method stub
                        NotificationsFragment fragment = new NotificationsFragment();
                        GlobalViewModel global =  GlobalViewModel.getGlobal();
                        List<Pets> selected = global.seleted.getValue();
                        List<Shopping> shoppingSelected = global.shoppingSelected.getValue();
                        if(isChecked){
                            //editText1.setText(buttonView.getText()+"选中");
                            selected.add(pets);
                            shoppingSelected.add(shopping);
                        }else{
                            //editText1.setText(buttonView.getText()+"取消选中");

                            selected.remove(pets);
                            shoppingSelected.remove(shopping);
                        }
                        global.seleted.setValue(selected);
                    }
                });
            };
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ShoppingViewHolder extends RecyclerView.ViewHolder {
        TextView shopping_shop_name;
        TextView shopping_name;
        TextView shopping_price;
        ImageView shopping_img;
        CheckBox select;

        public ShoppingViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            shopping_shop_name = itemView.findViewById(R.id.shopping_shop_name);
            shopping_name = itemView.findViewById(R.id.shopping_name);
            shopping_price = itemView.findViewById(R.id.shopping_price);
            shopping_img = itemView.findViewById(R.id.shopping_img);
            select = itemView.findViewById(R.id.select);
        }
    }

}
